package com.woniu.idearbac.Controller;

import com.woniu.idearbac.Service.DeptService;
import com.woniu.idearbac.dto.DeptDto;
import com.woniu.idearbac.entity.SysDept;
import com.woniu.idearbac.util.Result;
import com.woniu.idearbac.vo.DeptVo;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author King
 * @date 2022/11/25
 * @project boot_rbac
 */
@RestController
@RequestMapping("depts")
public class DeptController {
    @Resource
    private com.woniu.idearbac.Service.DeptService DeptService;
    @PostMapping("selAll")
    public Result selAll(@RequestBody DeptDto DeptDto){
        List<DeptVo> sysDepts = DeptService.selAll(DeptDto);
        return Result.ok().data("deptList",sysDepts);
    }

    @GetMapping("{id}")
    public Result selOne(@PathVariable Long id){
        SysDept sysDept = DeptService.selectByPrimaryKey(id);
       return Result.ok().data("list",sysDept);
    }

    @GetMapping("")
    public Result selAll(){
        List<SysDept> sysDepts = DeptService.selAllHome();
        return Result.ok().data("list",sysDepts);
    }

    @PutMapping("")
    public Result update(@RequestBody SysDept SysDept){
        int i = DeptService.updateByPrimaryKeySelective(SysDept);
        if(i > 0){
            return Result.ok();
        }else{
            return Result.error();
        }
    }
    @DeleteMapping("{id}")
    public Result del(@PathVariable Long id){
        SysDept sysDept = DeptService.selCount(id);
        if(sysDept.getId() != 0){
            return Result.error("该类型下有员工，不允许删除");
        }else{
            int i = DeptService.deleteByPrimaryKey(id);
            if(i > 0){
                return Result.ok();
            }else{
                return Result.error();
            }
        }

    }
    @PostMapping("")
    public Result add(@RequestBody SysDept SysDept){
        int i = DeptService.insertSelective(SysDept);
        if(i > 0){
            return Result.ok();
        }else{
            return Result.error();
        }
    }


}
